package com.eightbears.beboy.base.view;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Scroller;
import android.widget.TextView;

import com.eightbears.beboy.R;
import com.eightbears.beboy.base.util.DensityUtil;

public class LeftSlideView extends RelativeLayout {

    //侧滑方向-从哪侧滑出
    public static enum Positon {
        LEFT, RIGHT
    }

    private Context mContext;
    private Scroller mScroller = null;
    //侧滑菜单布局View
    private View mMenuView;
    //底部蒙层View
    private View mMaskView;
    private int mMenuWidth = 0;
    //屏幕宽度
    private int mScreenWidth = 0;
    //是否在滑动中
    private boolean mIsMoving = false;
    //显示登录界面与否
    private boolean mShow = false;
    //滑动动画时间
    private int mDuration = 600;
    //缺省侧滑方向为左
    private Positon mPositon = Positon.LEFT;
    private DensityUtil densityUtil;
    private OnCloseLeftMenuListener mOnCloseLeftMenuListener;

    public LeftSlideView(Context context) {
        this(context, null);
    }

    public LeftSlideView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LeftSlideView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    /**
     * 创建侧滑菜单View
     */
    public static LeftSlideView create(Activity activity) {
        LeftSlideView view = new LeftSlideView(activity);
        return view;
    }

    /**
     * 创建侧滑菜单View
     */
    public static LeftSlideView create(Activity activity, Positon positon) {
        LeftSlideView view = new LeftSlideView(activity);
        view.mPositon = positon;
        return view;
    }

    private void init(Context context) {
        mContext = context;
        densityUtil = new DensityUtil(mContext);
        setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
        setFocusable(true);
        mScroller = new Scroller(context);
        mScreenWidth = densityUtil.getScreenWidthPx();
        mMenuWidth = DensityUtil.dip2px(250);
        attachToContentView((Activity) context, mPositon);
    }

    /**
     * 创建 蒙层View并添加到contentView中
     */
    private void attachToContentView(Activity activity, Positon positon) {
        mPositon = positon;
        ViewGroup contentFrameLayout = (ViewGroup) activity.findViewById(android.R.id.content);
        ViewGroup contentView = ((ViewGroup) contentFrameLayout.getChildAt(0));
        mMaskView = new View(activity);
        mMaskView.setBackgroundColor(mContext.getResources().getColor(R.color.transparent_66000));
        contentView.addView(mMaskView, contentView.getLayoutParams());
        mMaskView.setVisibility(View.GONE);
        mMaskView.setClickable(true);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mMaskView.getLayoutParams();
        params.setMargins(0, DensityUtil.dip2px(50), 0, DensityUtil.dip2px(55));
        mMaskView.setLayoutParams(params);
        mMaskView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnCloseLeftMenuListener.onCloseLeft();
            }
        });
    }

    /**
     * 设置侧滑菜单View的宽度
     */
    public void setMenuWidth(int width) {
        ViewGroup.LayoutParams params = this.getLayoutParams();
        params.width = width;
        mMenuWidth = width;
        this.setLayoutParams(params);
    }


    /**
     * 设置侧滑菜单View，并添加到DectorView->LinearLayout->内容显示区域View中
     */
    public void setMenuView(Activity activity, View view) {
        Activity activity1 = activity;
        mMenuView = view;
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        addView(mMenuView, params);
        requestLayout();
        mMenuView.post(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                mMenuWidth = mMenuView.getWidth();
                switch (mPositon) {
                    case LEFT:
                        LeftSlideView.this.scrollTo(mScreenWidth, 0);
                        break;
                    case RIGHT:
                        LeftSlideView.this.scrollTo(-mScreenWidth, 0);
                        break;
                }

            }
        });
        ViewGroup contentFrameLayout = (ViewGroup) activity.findViewById(android.R.id.content);
        ViewGroup contentView = contentFrameLayout;
        contentView.addView(this);
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) this.getLayoutParams();
        switch (mPositon) {
            case LEFT:
                layoutParams.gravity = Gravity.LEFT;
                layoutParams.leftMargin = 0;
                break;
            case RIGHT:
                layoutParams.gravity = Gravity.RIGHT;
                layoutParams.rightMargin = 0;
                break;
        }
        TextView titleFrameLayout = (TextView) activity.findViewById(android.R.id.title);
        if (titleFrameLayout != null) {
            layoutParams.topMargin = densityUtil.getStatusBarHeight();
        }
        int flags = activity1.getWindow().getAttributes().flags;
        int flag = (flags & WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        if (flag == WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) {
            //说明状态栏使用沉浸式
            layoutParams.topMargin = densityUtil.getStatusBarHeight();
        }
        layoutParams.setMargins(0, DensityUtil.dip2px(50), 0, DensityUtil.dip2px(55));
        this.setLayoutParams(layoutParams);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (isShow()) {
                    return true;
                }
            default:
                break;
        }
        return super.onTouchEvent(event);
    }

    /**
     * 显示侧滑菜单View
     */
    public void show() {
        if (isShow() && !mIsMoving)
            return;
        switch (mPositon) {
            case LEFT:
                startScroll(DensityUtil.dip2px(250), -mMenuWidth, mDuration);
                switchMaskView(true);
                mShow = true;
                break;
            case RIGHT:
                startScroll(-mMenuWidth, mMenuWidth, mDuration);
                break;
        }

    }


    private void closeMaskView() {
        mMaskView.setVisibility(View.GONE);
    }

    /**
     * 蒙层显示开关
     */
    private void switchMaskView(boolean bShow) {
        if (bShow) {

            mMaskView.setVisibility(View.VISIBLE);
            Animation animation = new AlphaAnimation(0.0f, 1.0f);
            animation.setDuration(mDuration);
            mMaskView.startAnimation(animation);
        } else {
            mMaskView.setVisibility(View.GONE);
            Animation animation = new AlphaAnimation(1.0f, 0.0f);
            animation.setDuration(150);
            mMaskView.startAnimation(animation);
        }
    }

    /**
     * 关闭侧滑菜单View
     */
    public void dismiss() {
        if (!isShow() && !mIsMoving)
            return;
        switch (mPositon) {
            case LEFT:
                startScroll(LeftSlideView.this.getScrollX(), mMenuWidth, 300);
                switchMaskView(false);
                mShow = false;
                break;
            case RIGHT:
                startScroll(LeftSlideView.this.getScrollX(), -mMenuWidth, mDuration);
                break;
        }

    }

    public void close() {
        if (!isShow() && !mIsMoving)
            return;
        switch (mPositon) {
            case LEFT:
                startScroll(LeftSlideView.this.getScrollX(), mMenuWidth, 0);
                closeMaskView();
                mShow = false;
                break;
            case RIGHT:
                startScroll(LeftSlideView.this.getScrollX(), -mMenuWidth, 0);
                break;
        }

    }

    public boolean isShow() {
        return mShow;
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            // 更新界面
            postInvalidate();
            mIsMoving = true;
        } else {
            mIsMoving = false;
        }
        super.computeScroll();
    }

    /**
     * 拖动移动
     */
    public void startScroll(int startX, int dx, int duration) {
        mIsMoving = true;
        mScroller.startScroll(startX, 0, dx, 0, duration);
        invalidate();
    }

    // 功能条显示影藏动画
    public void changeView(boolean isShow) {
        ValueAnimator valueAnimator;
        if (isShow) {
            if (getWidth() == mScreenWidth)
                return;
            valueAnimator = ValueAnimator.ofInt(DensityUtil.dip2px(250), mScreenWidth);
        } else {
            if (getWidth() == (DensityUtil.dip2px(250)))
                return;
            valueAnimator = ValueAnimator.ofInt(mScreenWidth, DensityUtil.dip2px(250));
        }

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int w = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams params = LeftSlideView.this.getLayoutParams();
                params.width = w;
                mMenuWidth = w;
                LeftSlideView.this.setLayoutParams(params);
            }
        });
        valueAnimator.setDuration(100);
        //开始动画
        valueAnimator.start();
    }

    public interface OnCloseLeftMenuListener {
        void onCloseLeft();
    }

    public void setOnCloseLeftMenuListener(OnCloseLeftMenuListener onCloseLeftMenuListener) {
        this.mOnCloseLeftMenuListener = onCloseLeftMenuListener;
    }

}
